package game.client;

import game.client.net.ConnectionListener;
import game.client.net.GameConnection;
import game.client.net.ObjectPositionListener;
import game.common.util.Logger;
import game.common.world.Vector;

import java.io.IOException;

public class ClientBoot implements ConnectionListener {	
	/* ---------------- PARAM SYNTAX ---------------- */
	public static final String PARAM_HOST = "-h";
	public static final String PARAM_PORT = "-p";
	
	/* --------------- DEFAULT VALUES --------------- */
	public static final String DEFAULT_HOST = "localhost";
	public static final int DEFAULT_PORT = 5353;
	
	/* --------------- PARAMETRIZABLE --------------- */
	private String host;
	private int port;
	
	/* ------------------ UTILITY ------------------- */
	private Logger log = Logger.getLogger(getClass());
	
	/* -------------- CLIENT COMPONENTS ------------- */
	private GameConnection connection;
	
	/* ---------------- SERVER DATA ----------------- */
	private String initialPlayerName;
	private Vector initialPlayerPosition;
	
	/* -------------------- IMPL -------------------- */
	public ClientBoot(String[] args) {
		
		// set default values
		host = DEFAULT_HOST;
		port = DEFAULT_PORT;
		
		readArgs(args);
	}

	private void readArgs(String[] args){
		int offset = 0; 
		
		while (offset < args.length){
			if (args[offset].equals(PARAM_HOST)){
				host = args[offset + 1];
				offset += 2;
			} else if (args[offset].equals(PARAM_PORT)){
				port = Integer.parseInt(args[offset + 1]);
				offset += 2;
			} else {
				log.debug("unknown param '" + args[offset] + "'");
				++offset;
			}
		}
	}
	
	public void loadResources(){

		registerShutdownHook();
	}
	
	public void closeResources(){
		
	}
	
	public void loadWorld(){
		
	}
	
	private ObjectPositionListener createWorldUpdater(){
		return null;
	}
	
	public synchronized void startNetwork(){
		try {
			log.debug("opening connection " + host + ":" + port);
			
			this.connection = new GameConnection(host, 
												 port, 
												 this, 
												 createWorldUpdater());
			
			// waits for test first reply
			wait();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	@Override
	public synchronized void connected(String playerName, Vector playerVector) {
		log.debug("connected as " + playerName);
		
		this.initialPlayerName = playerName;
		this.initialPlayerPosition = playerVector;
		
		this.connection.setName(playerName);
		
		// wake up main thread
		this.notify();
	}
	
	public void loadPlayer(){
		if (this.initialPlayerName == null || this.initialPlayerPosition == null){
			throw new IllegalStateException("initial player values not initialized");
		}
	}
	
	public void registerShutdownHook(){
		Runtime.getRuntime().addShutdownHook(new Thread(){
			
			public void run(){
				closeResources();
			}
		});
	}
	
	public static void main(String[] args) {
		ClientBoot boot = new ClientBoot(args);
		boot.loadResources();
		boot.loadWorld();
		
		boot.startNetwork();
		
		boot.loadPlayer();
	}
}
